package com.zhuge_new.Activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.bean.ShareBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.BitmapUtils;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.GlideUtils;
import com.zhuge_new.untils.PreferencesUtils;
import com.zhuge_new.untils.StatusBarUtil;
import com.zhuge_new.wxapi.Constacts;

/**
 * Created by Administrator on 2018/11/13.
 */

public class MinShareActivity extends BaseActivity implements View.OnClickListener {

    private ImageView share_logo;
    private ImageView btn_share;
    private IWXAPI wxApi;
    private String shareurl = "http://web.zgzngj.com";
    ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_minshare);
        StatusBarUtil.transparencyBar(MinShareActivity.this);
        init_title();

    }

    @Override
    public void init_title() {
        super.init_title();
        wxApi = WXAPIFactory.createWXAPI(baseContext, Constacts.WXAPPID);
        wxApi.registerApp(Constacts.WXAPPID);
        share_logo = findViewById(R.id.share_logo);
        btn_share = findViewById(R.id.btn_share);
        btn_share.setOnClickListener(this);
        getData();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_share:
                getShare_Dialog();
                break;
        }
    }

    public void getData() {
        shareurl =BaseHttp.basecard+"/m/card/apply?openid="+
                PreferencesUtils.getString(baseContext,"unionid")+"?token="+
                PreferencesUtils.getString(baseContext,"token");
        Bitmap bitmap = BitmapUtils.localityBitmap(baseContext, R.mipmap.ic_launcher);
        share_logo.setImageBitmap(CodeUtils.createImage(shareurl, 500, 500, bitmap));
    }

    //一个bitmap对象，一个发送位置
    public void imageShare(Bitmap bmp, int sendtype) {

        WXImageObject imgObj = new WXImageObject();

        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 1, 1, true);
        msg.setThumbImage(thumbBmp);
        bmp.recycle();
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = sendtype == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;

        wxApi.sendReq(req);
    }

    private void sharewx(int flag) {
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = shareurl;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = "诸葛智能管家";
        msg.description = "我发现一款超级好玩的APP,赶快下载抢红包";
        Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
        msg.setThumbImage(thumb);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        wxApi.sendReq(req);
    }

    public void getShare_Dialog() {
        String[] stringItems = {"分享给好友", "分享朋友圈"};
        final ActionSheetDialog dialog = new ActionSheetDialog(this, stringItems, null);
        dialog.title("请选择选择分享方式")//
                .titleTextSize_SP(14.5f)//
                .show();
        dialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        sharewx(0);
                        dialog.dismiss();
                        break;
                    case 1:
                        sharewx(1);
                        dialog.dismiss();
                }
            }
        });
    }



}
